<template>
	<view class="myposts-content" v-if="loadEnd">
		<!-- 头部 -->
		<titleBar :show-data="title_data" @change="getVal"></titleBar>
		<!-- 主体内容 -->
		<view class="myposts-main">
			<mainList :show-data="postsList" type="myposts" :status="newIndex" @likePosts="likePosts" @collections="collections" @delPosts="delPosts"></mainList>
		</view>
		<view class="" style="padding: 0 0 10rpx;color: #656565;font-size: 26rpx;text-align: center;" v-if="postsList.length != 0 && !hasMore">没有更多了~</view>
		<view class="safe-padding"></view>
	</view>
</template>

<script>
import ikz from '@/pages/Common/tools.js'

import mainList from '@/pages/Component/forum/component/main-list/mainList.vue'
import titleBar from '@/pages/Component/forum/component/title/title-bar.vue'
export default {
	components: {
		mainList,
		titleBar,
	},
	data() {
		return {
			loadEnd: false,
			newIndex: 1,
			postsList: [],
			title_data: [
				{
					id: 1,
					name: '待审核',
				},
				{
					id: 2,
					name: '已发布',
				},
				{
					id: 3,
					name: '已驳回',
				},
			],
			page: 1,
			limit: 5,
			hasMore: false,
		}
	},
	onLoad() {
		this.loadEnd = false
		this.getMyPosts()
	},
	onPullDownRefresh() {
		this.page = 1
		this.postsList = []
		this.getMyPosts()
	},
	onReachBottom() {
		if (this.hasMore) {
			this.page++
			this.getMyPosts()
		}
	},
	methods: {
		// 子父组件通信
		getVal(e) {
			if (this.newIndex == e) {
				return
			} else {
				this.postsList = []
				this.newIndex = e
				this.getMyPosts()
			}
		},
		// 获取我的帖子列表
		getMyPosts() {
			uni.showLoading({
				mask: true,
			})
			let path = '/xyb/posts/posts/user_posts'
			let data = {
				token: uni.getStorageSync('local_user_session'),
				page: this.page,
				limit: this.limit,
				push_status: this.newIndex,
			}
			ikz.requestPost(path, data, res => {
				// console.log(res);
				if (res.statusCode == 200) {
					let postsList = res.data.data.data
					postsList.forEach(el => {
						if (el.images != '') {
							// el.images = el.images.split(',')
							el.imagesNum = el.images.length
							if (el.imagesNum > 3) {
								el.images.length = 3
							}
						}
					})
					this.postsList = this.page == 1 ? postsList : [...this.postsList, ...postsList]
					this.hasMore = this.page < res.data.data.count / this.limit ? true : false
				}
				this.loadEnd = true
				uni.hideLoading()
				uni.stopPullDownRefresh()
			})
		},
		// 已发布帖子点赞
		likePosts(id, status) {
			if (this.newIndex == 2) {
				let path = ''
				if (status == 0) {
					path = '/xyb/posts/posts/give'
				} else {
					path = '/xyb/posts/posts/give_del'
				}
				let data = {
					token: uni.getStorageSync('local_user_session'),
					posts_id: id,
				}
				ikz.requestPost(path, data, res => {
					if (res.statusCode == 200) {
						// console.log(res.data.data);
						this.postsList.forEach(el => {
							if (el.id == id) {
								if (el.give_status == 0) {
									el.give_status = 1
									el.give++
								} else {
									el.give_status = 0
									el.give--
								}
							}
						})
						let title = status == 0 ? '点赞成功' : '取消点赞'
						uni.showToast({
							title,
							icon: 'none',
						})
					}
				})
			}
		},
		// 已发布帖子收藏
		collections(id, status) {
			if (this.newIndex == 2) {
				let path = ''
				if (status == 0) {
					path = '/xyb/posts/posts/collection'
				} else {
					path = '/xyb/posts/posts/collection_del'
				}
				let data = {
					token: uni.getStorageSync('local_user_session'),
					posts_id: id,
				}
				ikz.requestPost(path, data, res => {
					if (res.statusCode == 200) {
						this.postsList.forEach(el => {
							if (el.id == id) {
								if (el.collection_status == 0) {
									el.collection_status = 1
									el.collection++
								} else {
									el.collection_status = 0
									el.collection--
								}
							}
						})
						let title = status == 0 ? '收藏成功' : '取消收藏'
						uni.showToast({
							title,
							icon: 'none',
						})
					}
				})
			}
		},
		// 删除帖子
		delPosts(id) {
			uni.showLoading({
				mask: true,
			})
			let path = '/xyb/posts/posts/del'
			let data = {
				id,
				token: uni.getStorageSync('local_user_session'),
			}
			ikz.request(path, data, res => {
				if (res.statusCode == 200) {
					uni.showToast({
						title: '删除成功',
						icon: 'none',
					})
					this.postsList.forEach((el, index) => {
						if (el.id == id) {
							this.$delete(this.postsList, index)
						}
					})
				}
				uni.hideLoading()
			})
		},
	},
}
</script>

<style lang="scss">
page {
	background: #f6f7fb;
}

.myposts-content {
	.myposts-main {
		margin-top: 20rpx;
	}
}
</style>
